package sixkey.controller;

/**
 * ClassName:JustAuthController
 * Package:sixkey.controller
 * Description
 *
 * @Author:@wenxueshi
 * @Create:2023/6/21 - 23:35
 * @Version:v1.0
 */

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.model.AuthCallback;
import me.zhyd.oauth.request.AuthGiteeRequest;
import me.zhyd.oauth.request.AuthRequest;
import me.zhyd.oauth.utils.AuthStateUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import sixkey.utils.ResResult;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 集成第三方登录
 */
@Api(tags = "gitee登录")
@RestController
@RequestMapping("/oauth")
public class JustAuthController {

    /**
     * 让客户端直接此接口即可
     * @param response
     * @throws IOException
     */
    @RequestMapping("/render")
    public void renderAuth(HttpServletResponse response) throws IOException {
        AuthRequest authRequest = getAuthRequest();
        String authorize = authRequest.authorize(AuthStateUtils.createState());
        response.sendRedirect(authorize);
    }

    @RequestMapping("/callback")
    public Object login(AuthCallback callback) {
        AuthRequest authRequest = getAuthRequest();
        return authRequest.login(callback);
    }

    private AuthRequest getAuthRequest() {
        return new AuthGiteeRequest(AuthConfig.builder()
                .clientId("158c83ef36e43edbe21c855c234d3ec8cca10633d7c0fcc9425bcae32ad34770")
                .clientSecret("81f7b13bfa965e241935f6de97e5cf6c455cb8a7bcefd1507a06ab6874119aa9")
                .redirectUri("http://8.130.108.52/")
                .build());
    }
}
